﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CubeStone.Wang.Utilities
{
    /// <summary>
    /// Base64加解密类
    /// </summary>
    public class Base64
    {

        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="strIn">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string strIn)
        {
            return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(strIn));
        }

        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="strIn">需要解密的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string Decrypt(string strIn)
        {
            switch (strIn.Length % 4)
            {
                case 1:
                    {
                        strIn += "===";
                        break;
                    }
                case 2:
                    {
                        strIn += "==";
                        break;
                    }
                case 3:
                    {
                        strIn += "=";
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            return System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(strIn));
        }
    }
}